﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaobaoERP.Core.Common;
using System.Collections;
using TaobaoERP.BusinessTaobao.Common;
using TaobaoERP.BusinessTaobao.Core;
using System.Xml;
using System.Diagnostics;
using TaobaoERP.Entity;
using System.Collections.Specialized;
using TaobaoERP.BusinessTaobao.ParObject;

namespace TaobaoERP.BusinessTaobao.TaobaoAPI
{
    public class TaobaoAPI : MarshalByRefObject
    {
        private string sip_sessionid;
        public TaobaoAPI(string SessionID)
        {
            sip_sessionid = SessionID;
        }
        public string login()
        {
            return  Constants.HEADER_SIP_ISP_LOGINURL + "?sip_apiname=taobao.trades.sold.get&sip_appkey=" + AppSettings.APP_ID + "&sip_sessionid=" + sip_sessionid + "&sip_redirecturl=http://localhost:1111/Home/";
        }
        /// <summary>
        /// 此接口方法以实现批量得到用户公开资料。
        /// </summary>
        /// <returns></returns>
        public string getUsers()
        {
            string apiname = "taobao.users.get";
            return apiname;
        }
        /// <summary>
        /// 此接口方法以实现得到单个用户资料
        /// </summary>
        /// <returns></returns>
        public string getUser(string nick)
        {
            string apiname = "taobao.user.get";
            string timestamp = System.DateTime.Now.ToString(Constants.SIP_TIMESTAMP_FORMATER);//时间获得当前系统时间
            string appid = AppSettings.APP_ID;
            SortedList mySL = new SortedList();
            mySL.Add("sip_appkey", AppSettings.APP_ID);
            mySL.Add("sip_timestamp", timestamp);
            mySL.Add("sip_apiname", apiname);
            mySL.Add("v", "1.0");
            mySL.Add("nick", nick);
            mySL.Add("fields", "id,nick,location");
            string QueryString = Signature.DoSignature(mySL);
            string status = string.Empty;
            string result = Signature.Post(Constants.SIP_API_URL, QueryString, out status);
            if (string.IsNullOrEmpty(status))
            {
                return "error";
            }
            else
            {
                if ("9999".Equals(status))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(result);
                    XmlNode xn = xmlDoc.SelectSingleNode("StringResult/result");
                    if (xn != null)
                    {
                        XmlElement xe = (XmlElement)xn;
                        return xe.InnerText;
                    }
                    else
                        return "未找到用户";
                }
                else
                {
                    return Signature.GetMessage(status);
                }
            }
        }
      
        

        

        

        /// <summary>
        ///  	此接口方法以实现得到当前会话用户（必须为卖家）的出售中商品列表。支持分页。。
        /// </summary>
        /// <param name="Nick"></param>
        /// <returns></returns>
        public List<SellerCat> instock_list_get(string Nick)
        {
            //TODO: 未测试
            string apiname = "taobao.items.instock.get";
            string timestamp = System.DateTime.Now.ToString(Constants.SIP_TIMESTAMP_FORMATER);//时间获得当前系统时间
            string appid = AppSettings.APP_ID;
            SortedList mySL = new SortedList();
            mySL.Add("sip_appkey", AppSettings.APP_ID);
            mySL.Add("sip_timestamp", timestamp);
            mySL.Add("sip_apiname", apiname);
            mySL.Add("sip_sessionid", sip_sessionid);
            mySL.Add("v", "1.0");

            mySL.Add("fields", Nick);
            //mySL.Add("sip_format", "json");
            string QueryString = Signature.DoSignature(mySL);
            string status = string.Empty;
            string result = Signature.Post(Constants.SIP_API_URL, QueryString, out status);


            if (string.IsNullOrEmpty(status))
            {
                return null;
            }
            else if ("9999".Equals(status))
            {

                NameValueCollection par = new NameValueCollection();
                par.Add("ID", "title");
                par.Add("Name", "name");
                par.Add("ParentID", "parent_cid");

                return new XmlHandler<SellerCat>().ListItemByXml(result, "rsp/item", par);
            }
            else if ("1004".Equals(status))
            {
                System.Web.HttpContext.Current.Response.Redirect(result + "&sip_redirecturl=http://localhost:1111/Home/");
                return null;
            }
            else
            {
                Utils.Debug("sellercats_list_get:  " + Nick + " : " + Signature.GetMessage(status));
                return null;
            }

        }

        
    }
}
